
package com.epam.android.training.level1.widget;

import com.epam.android.training.level1.R;
import com.epam.android.training.level1.db.dao.RssDao;
import com.epam.android.training.level1.db.dao.impl.RssDaoImpl;
import com.epam.android.training.level1.rss.NewsFeed;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @author Anton Novikau
 */
public class FeedPreference extends Preference {

  private final RssDao rssDao;

  private FeedAdapter adapter;

  public FeedPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    rssDao = new RssDaoImpl(context);
    setWidgetLayoutResource(R.layout.feed_preference);
  }

  @Override
  protected void onBindView(View view) {
    super.onBindView(view);

    ListView feedContainer = (ListView) view.findViewById(R.id.pref_feed_container);
    feedContainer.setItemsCanFocus(false);
    adapter = new FeedAdapter(rssDao.findAllFeeds());
    feedContainer.setAdapter(adapter);

    // Button deleteButton = (Button) view.findViewById(R.id.delete_feed);
    // deleteButton.setOnClickListener(new View.OnClickListener() {
    //
    // @Override
    // public void onClick(View v) {
    // // TODO Auto-generated method stub
    //
    // }
    // });
    // Button addButton = (Button) view.findViewById(R.id.add_feed);
    // addButton.setOnClickListener(new View.OnClickListener() {
    //
    // @Override
    // public void onClick(View v) {
    // showEditTextDialog();
    // }
    // });
  }

  private AlertDialog showEditTextDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    final EditText editText = new EditText(getContext());
    builder.setMessage(R.string.feed_pref_dialog_title);
    builder.setView(editText);
    builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {
        String urlText = editText.getText().toString();
        try {
          URL feedUrl = new URL(urlText);
          long feedId = rssDao.createFeed(feedUrl);
          if (feedId > 0) {
            NewsFeed feed = rssDao.findFeed(feedId, true);
            adapter.addItem(feed);
          }
          dialog.dismiss();
        } catch (MalformedURLException e) {
          Context context = getContext();
          Toast.makeText(context, context.getString(R.string.bad_feed_url_message, urlText),
              Toast.LENGTH_LONG).show();
        }
      }
    });

    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {}
    });
    return builder.show();
  }

  private class FeedAdapter extends CollectionAdapter<NewsFeed> {
    private final LayoutInflater inflater;

    private final List<NewsFeed> items;

    public FeedAdapter(List<NewsFeed> items) {
      super(items);
      this.items = items;
      this.inflater = (LayoutInflater) getContext().getSystemService(
          Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      TextView title;
      if (convertView == null) {
        convertView = inflater.inflate(android.R.layout.simple_list_item_multiple_choice, parent,
            false);

        title = (TextView) convertView.findViewById(android.R.id.text1);

        convertView.setTag(title);
      } else {
        title = (TextView) convertView.getTag();
      }
      NewsFeed feed = getItem(position);
      title.setText(feed.getTitle());

      return convertView;
    }

    private void addItem(NewsFeed item) {
      items.add(item);
      notifyDataSetChanged();
    }
  }
}
